import numpy as np
from sklearn.feature_extraction import DictVectorizer
from sklearn.model_selection import cross_val_score
from sklearn.tree import DecisionTreeClassifier
import pandas as pd

#数据加载
test_data = pd.read_csv("./data/test.csv")
train_data = pd.read_csv("./data/train.csv")

#数据探索
# print(train_data.info())
# print("*"*50)
# print(test_data.info())
# print(train_data.describe())
# print("*"*50)
# print(train_data.describe(include=["O"]))
# print("*"*50)
# print(train_data.head(5))
# print("*"*50)
# print(train_data.tail(5))

#数据清洗
train_data["Age"].fillna(train_data["Age"].mean(),inplace=True)
test_data["Age"].fillna(test_data["Age"].mean(),inplace=True)
test_data["Fare"].fillna(test_data["Fare"].mean(),inplace=True)
train_data["Embarked"].fillna("S",inplace=True)
# print(train_data.info())
# print("*"*50)
# print(test_data.info())
# counts = train_data["Embarked"].value_counts()
# print(counts)

# 特征选择
features = ['Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare', 'Embarked']
train_feature = train_data[features]
train_lables = train_data["Survived"]
test_feature = test_data[features]

#特征转化，将特征向量转换为数值型特征矩阵
# print(train_feature)
div = DictVectorizer(sparse=False)
train_feature = div.fit_transform(train_feature.to_dict(orient='records'))
test_feature = div.fit_transform(test_feature.to_dict(orient='records'))
# print(div.feature_names_)
# print(train_feature)

#创建决策树模型
dtc = DecisionTreeClassifier(criterion='entropy')

#模型训练
dtc.fit(train_feature,train_lables)

#模型预测
pre_lables = dtc.predict(test_feature)

#模型评估，理论上要使用测试数据特征（test_feature）和测试数据的真实标签（test_lables），但是我们不知道真实标签，这里使用训练集和训练集真实标签
# res = round(dtc.score(train_feature,train_lables),6)
# print(f'模型准确率为：{res}')

#使用k折交叉验证，从新统计模型准确率
acc = round(np.mean(cross_val_score(dtc,train_feature,train_lables,cv=10)),4)
print(f'模型评估准确率为：{acc}')